﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>自动传输管理后台</title>
    <link href="~/css/layui.css" rel="stylesheet" />
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body class="main_body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header header">
            <a href="javascript:;" class="layui-logo" style="color:white">AutoSchedule</a>
            <ul class="layui-nav layui-layout-left">
                @*<li class="layui-nav-item layui-this">
                    <a href="javascript:;" style="color:seashell">事无对错</a>
                    <dl class="layui-nav-child">
                        <dd><a href="">事无对错</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item layui-this">
                    <a href="javascript:;" style="color:seashell">人分善恶</a>
                </li>
                <li class="layui-nav-item layui-this">
                    <a href="javascript:;" style="color:seashell">但行所愿</a>
                </li>*@
                @*<li class="layui-nav-item layui-this">
                    <a href="javascript:;" style="color:seashell">莫问前程</a>
                </li>*@
            </ul>
        </div>

        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <dl class="layui-nav-child">
                            <dd><a href="/Home/Index">首页</a></dd>
                            <dd><a href="/DataSource/DataSource">数据源设置</a></dd>
                            <dd><a href="/Organization/Organization">组织机构设置</a></dd>
                            <dd><a href="/SystemKey/SystemKey">系统变量设置</a></dd>
                            <dd><a href="/TaskPlan/TaskPlan">任务计划</a></dd>
                            <dd><a href="/Logs/Index">任务执行日志</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
        <div class="layui-body">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
        </div>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
        </div>
    </script>

    <script type="text/html" id="toolbardeleteLogs">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="deleteSelect">删除所选日志</button>
        </div>
    </script>

    <script type="text/html" id="toolbarDemoDetail">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">新增任务计划明细</button>
        </div>
    </script>
    <script type="text/html" id="barDemoTask">
        <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
        <button class="layui-btn layui-btn-xs" lay-event="begin">开启</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="stop">关闭</button>
    </script>

    <script type="text/html" id="barDemo">
        <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
    </script>

    <script type="text/html" id="barDemoDetail">
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
    </script>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="~/layui.js"></script>
    <script src="~/js/bank.js"></script>
    <script>
        //首页的三个按钮
        $("#begin").click(beginPlan);
        $("#stop").click(stopPlan);
        var tkguid = '';
        var table;
        layui.use('element', function () {
            var element = layui.element;
            element.on('tab(demo)', function (data) {
            });
        });
        layui.use(['form', 'layedit', 'laydate'], function () {
            var form = layui.form;
            form.on('submit(enSure)', function (data) {
                var dataDetail = JSON.stringify(data.field);
                console.log(dataDetail);
                $.ajax({
                    url: "/TaskPlan/TaskPlanDetailAdd",
                    async: true,
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json;charset=UTF-8",
                    data: dataDetail,
                    success: function (dataResult) {
                        if (dataResult.code != "0") {
                            alert(dataResult.msg);
                        }
                        var index = parent.layer.getFrameIndex(window.name);
                        setTimeout(function () { parent.layer.close(index) }, 1000);
                        setTimeout(function () { parent.location.reload() }, 100);
                    }
                });

            });
        });
        layui.use('table', function () {
            table = layui.table;
            //数据源设置表格
            table.render({
                elem: '#dataSource'
                , url: '/DataSource/DataSourceResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , id: 'datasourceDtId'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , cols: [[
                    //{ type: 'radio' },
                    { field: 'GUID', title: '编号', sort: true },
                    { field: 'Name', title: '名称',width:240, sort: true },
                    { field: 'GroupSqlString', title: '分组数据源', sort: true },
                    { field: 'SqlString', title: '数据源', sort: true },
                    { field: 'AfterSqlString', title: '调用正常后执行语句', sort: true },
                    { field: 'AfterSqlstring2', title: '调用异常后执行语句', sort: true },
                    {
                        field: 'IsStart', title: '状态', sort: true, width: 80, templet: function (d) {
                            if (d.IsStart == "1") {
                                return "<font color='green' size='3'> <b>已启用</b></font>";
                            } else if (d.IsStart == "0") {
                                return "<font color='red' size='3'> <b>未启用</b></font>";
                            }
                        }
                    },
                    { field: 'MainKey', title: '主关键字段', sort: true },
                    { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 178 }
                ]]
                , page: true
                , limit:90
            });

            //组织机构表格
            table.render({
                elem: '#organization'
                , url: '/Organization/OrgResult'
                , toolbar: '#toolbarDemo'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , cols: [[
                    //{ type: 'radio' },
                    //orgName = "123",orgNum = ""
                    { field: 'orgNum', title: '编号', sort: true },
                    { field: 'orgName', title: '名称', sort: true }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 178 }
                ]]
                , page: true
            });

            //系统变量设置表格
            table.render({
                elem: '#SystemKey'
                , toolbar: '#toolbarDemo'
                , url: '/SystemKey/SystemKeyResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , cols: [[
                    //{ type: 'radio' },
                    { field: 'KeyName', title: '关键字名称', sort: true },
                    { field: 'KeyValue', title: '关键字值', sort: true }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 178 }
                ]]
                , page: true
                , limit: 90
            });

            //任务计划表格
            table.render({
                elem: '#TaskPlan'
                , toolbar: '#toolbarDemo'
                , url: '/TaskPlan/TaskPlanResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , id: 'taskDtId'
                , cols: [[

                    { field: 'GUID', title: '任务编号', hide: true, sort: true },
                    { field: 'CODE', title: '任务编号', sort: true },
                    { field: 'Name', title: '任务名称', sort: true },
                    { field: 'Frequency', title: '执行频率', sort: true },
                    { field: 'OrgCode', title: '所属机构', sort: true },
                    { field: 'WorkType', title: '处理数据类型', sort: true },
                    { field: 'DllOrUrl', title: 'dll或api地址', sort: true },
                    {
                        field: 'Status', title: '任务状态', sort: true, templet: function (d) {
                            if (d.Status == "已开启") {
                                return "<font color='green' size='3'> <b>已开启</b></font>";
                            } else if (d.Status == "未开启") {
                                return "<font color='red' size='3'> <b>未开启</b></font>";
                            }
                        }, width: 120
                    },
                    { fixed: 'right', title: '操作', toolbar: '#barDemoTask', width: 220 }
                ]]
                , page: true
                , done: function (res) {
                    renderDone(res);
                }
            });

            //任务计划日志表格
            table.render({
                elem: '#logs'
                , toolbar: '#toolbardeleteLogs'
                , url: '/Logs/LogsResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , id: 'taskLogDtId'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    { field: 'Id', title: 'Id', hide: true, sort: true, width: 120 },
                    { field: 'EventName', title: '任务名称', hide: false, sort: true, width: 120 },
                    { field: 'Level', title: '日志等级', sort: true, width: 100 },
                    { field: 'Message', title: '日志内容', sort: true },
                    { field: 'Time', title: '时间', sort: true, width: 200 },
                    { fixed: 'right', title: '操作', toolbar: '#barDemoDetail', width: 178 }
                ]]
                , page: true
                , limit: 90
                , done: function (res) {
                    renderDone(res);
                }
            });

            var $ = layui.$, active = {
                reload: function () {
                    var searchReload = $('#searchReload');

                    //执行重载
                    table.reload('taskLogDtId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            value: searchReload.val()
                        }
                    }, 'data');
                }
            };

            $('.searchTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //点击任务计划，会显示相应的明细
            table.on('row(TaskPlan)', function (obj) {

                var data = obj.data;
                var guid = data.GUID;
                tkguid = data.GUID;
                table.render({
                    elem: '#TaskPlanDetail'
                    , toolbar: '#toolbarDemoDetail'
                    , url: '/TaskPlan/TaskPlanDetailResult?guid=' + guid
                    , cellMinWidth: 80
                    , cols: [[
                        { field: 'guid', title: 'tkDetailGuid', sort: true, hide: true },
                        { field: 'dsGuid', title: '编号', sort: true },
                        { field: 'dsName', title: '名称', sort: true },
                        { field: 'dsState', title: '是否启用', sort: true },
                        { fixed: 'right', title: '操作', toolbar: '#barDemoDetail', width: 178 }
                    ]]
                    , page: true
                });
                //标注选中样式
                obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            });

            // 数据源新增
            table.on('toolbar(dataSource)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                if (obj.event == 'add') {
                    layer.open({
                        type: 2,
                        title: '新增数据源',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/DataSource/DataSourceAdd' 
                    });
                }
            });

            //数据源编辑
            table.on('tool(dataSource)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/DataSource/DataSourceDelete?GUID=" + data.GUID,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                } else {
                                    alert(dataResult.msg)
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '编辑数据源',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/DataSource/DataSourceEdit?guid=' + data.GUID //iframe的url
                    });
                }
            });

            // 系统变量新增
            table.on('toolbar(SystemKey)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                if (obj.event == 'add') {
                    layer.open({
                        type: 2,
                        title: '新增系统变量',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/SystemKey/SystemKeyAdd' //iframe的url
                    });
                }
            });

            //系统变量编辑
            table.on('tool(SystemKey)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/SystemKey/SystemKeyDelete?KeyName=" + data.KeyName,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '编辑数据源',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/SystemKey/SystemKeyEdit?KeyName=' + data.KeyName //iframe的url
                    });
                }
            });

            // 任务计划新增
            table.on('toolbar(TaskPlan)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                if (obj.event == 'add') {
                   layer.open({
                        type: 2,
                        title: '新增任务计划',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/TaskPlan/TaskPlanAdd' //iframe的url
                    });
                }
            });

            //任务计划编辑
            table.on('tool(TaskPlan)', function (obj) {
                //console.log(obj);
                var data = obj.data;
                if (obj.event === 'del') {
                    if (data.Status == '已开启') {
                        alert('任务已经开启，请停止后删除')
                    }
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/TaskPlan/TaskPlanDelete?GUID=" + data.GUID,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        });
                    });
                }
                else if (obj.event === 'edit') {
                    if (data.Status == '已开启') {
                        alert('任务已经开启，请停止后编辑')
                    }
                    else {
                        layer.open({
                            type: 2,
                            title: '编辑任务计划',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['100%', '100%'],
                            content: '/TaskPlan/TaskPlanEdit?GUID=' + data.GUID //iframe的url
                        });
                    }
                    
                }
                else if (obj.event === 'begin') {
                    $.ajax({
                        url: "/Home/BeginTaskPlan?guid=" + data.GUID,
                        async: true,
                        type: "Get",
                        dataType: "json",
                        contentType: "application/json;charset=UTF-8",
                        success: function (dataResult) {
                            if (dataResult.code == '0') {
                                data.State = '已开启';
                                $('.alert').html(dataResult.msg).addClass('alert-success').show().delay(3000).fadeOut();
                            } else {
                                $('.alert').html(dataResult.msg).addClass('alert-false').show().delay(6000).fadeOut();
                            }
                            table.reload('taskDtId');
                        }
                    });
                }
                else if (obj.event === 'stop') {
                    $.ajax({
                        url: "/Home/StopTaskPlan?guid=" + data.GUID,
                        async: true,
                        type: "Get",
                        dataType: "json",
                        contentType: "application/json;charset=UTF-8",
                        success: function (dataResult) {
                            if (dataResult.code == '0') {
                                data.State = '未开启';
                                $('.alert').html(dataResult.msg).addClass('alert-success').show().delay(4000).fadeOut();
                            } else {
                                $('.alert').html(dataResult.msg).addClass('alert-success').show().delay(4000).fadeOut();
                            }
                            table.reload('taskDtId');
                        }
                    });
                }
            });

            // 任务计划明细新增
            table.on('toolbar(TaskPlanDetail)', function (obj) {
                if (obj.event == 'add') {
                    layer.open({
                        type: 2,
                        title: '新增任务计划明细',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['700px', '500px'],
                        content: '/TaskPlan/TaskPlanDetailAdd?tkguid=' + tkguid
                    });
                }
            });

            //任务计划明细编辑
            table.on('tool(TaskPlanDetail)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/TaskPlan/TaskPlanDetailDelete?GUID=" + data.tkDetailGuid,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '编辑任务计划明细',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/TaskPlan/TaskPlanDetailEdit?GUID=' + data.tkDetailGuid //iframe的url
                    });
                }
            });

            // 组织机构新增
            table.on('toolbar(organization)', function (obj) {
                if (obj.event == 'add') {
                    layer.open({
                        type: 2,
                        title: '新增组织机构',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/Organization/OrgAdd' //iframe的url
                    });
                }
            });

            //组织机构编辑
            table.on('tool(organization)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/Organization/OrgDelete?orgNum=" + data.orgNum,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        });
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '组织结构修改',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '/Organization/OrgEdit?orgNum=' + data.orgNum 
                    });
                }
            });

            //日志单个删除
            table.on('tool(logs)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: "/Logs/LogDelete?logNum=" + data.Id,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    obj.del();
                                    layer.close(index);
                                }
                            }
                        });
                    });
                }
            });

            table.on('toolbar(logs)', function (obj) {
                 var logsid = '';
                if (obj.event === 'deleteSelect') {
                    var checkStatus = table.checkStatus('taskLogDtId');
                   
                    for (i = 0; i < checkStatus.data.length; i++) {
                        if (logsid.length ==0) {
                            logsid = checkStatus.data[i].Id;
                        } else {
                             logsid = logsid +',' + checkStatus.data[i].Id;
                        }
                       
                    };
                    
                    layer.confirm('确定删除所选的' + checkStatus.data.length+ '条日志?', function (index) {
                        $.ajax({
                            url: "/Logs/LogDelete?logNum=" + logsid,
                            async: true,
                            type: "Get",
                            dataType: "json",
                            contentType: "application/json;charset=UTF-8",
                            success: function (dataResult) {
                                if (dataResult.code == "0") {
                                    table.reload("taskLogDtId");
                                    layer.close(index);
                                } else {
                                    alert(dataResult.msg)
                                }
                            }
                        });
                    });
                }
            });
        });

    </script>
</body>
</html>